var rowIdRet = "";
var FaultName = "";
var repairId ="";
var repairNumber ="";//维修单编号
var repairTitle = "";//故障标题
var repairDate = "";//维修时间
var repairDateDoor ="";//维修具体时间
var openId="";
var familyName = "";
var hInfo = "";
var servicePhone = "";
$(function(e){

	dataInit();
});

function dataInit(){
	eventInt();
	$('#messageTable').bootstrapTable({
		url: path+'e4e7f020a08e4973b9d8870ee57e7f46', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset, searchParams:$("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
        			return index+1;
        		}}, 
        		{field: 'repairNumber', title: '报修单编号', align: 'center', width: '150', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
        		{field: 'repairTitle', title: '故障标题', align: 'center', width: '150', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'repairText', title: '故障内容', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'repairImg', title: '故障现场图片', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}else{
	            		value = 'http://daxingzhiye.com/wechatPre/'+value;
	            	}
	            	return '<img style="width:40px;height:40px;" src="'+value+'"/>';
	            }},
	            {field: 'createTime', title: '报修时间', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'faultState', title: '维修状态', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}else{
	            		if(value == 1){
	            			value ='<p style="color:#B452CD">'+'新提交维修单'+'</p>';
	            		}if(value == 2){
	            			value = '<p style="color:#FA8072">'+'已通知维修人员'+'</p>';
	            		}if(value == 3){
	            			value = '<p style="color:#71C671">'+'维修完成'+'</p>';
	            		}
	            	}
	            	
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'name', title: '维修人员', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            	
	            }},
	            {field: 'type', title: '报修类型', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		return '<a style="word-wrap:break-word;">-</a>';
	            	}else{
	            		if(value == 1){
	            			return '<a style="word-wrap:break-word;color:green">个人报修</a>';
	            		}else if(value == 2){
	            			return '<a style="word-wrap:break-word;color:blue">公共报修</a>';
	            		}
	            	}
	            	
	            	
	            }},
	            {field: 'operate', title: '操作', align: 'center', width: '210', events: {
	            	 'click .RoleOfA': function (e, value, row, index) {
	            		 rowIdRet = row.id;
	            		 FaultName = row.faultName;
	            		 repairNumber = row.repairNumber;
	            		 repairTitle = row.repairTitle;
	            		 repairDate = row.repairDate;
	            		 repairDateDoor = row.repairDateDoor;
	            		 openId = row.openId;
	            		 familyName = row.familyName;
	            		 hInfo = row.hInfo;
	            		 servicePhone = row.phone;
	            		 		//修改维修状态
	            		 		editRepair(row.id);

		                	
		                },
		                'click .RoleOfB': function (e, value, row, index) {
		                	qiao.bs.confirm("确定要删除此项吗？",function(){
		                		
			                	deleteFault(row.id);
			                },function(){});
		                },
		                'click .RoleOfC': function (e, value, row, index) {
		                	//报修单详情
		                	findRepairInfo(row.id);
		                },
	            },
	            formatter: function (value, row, index) {
	            	return [
	            	    '<button type="button" class="RoleOfA btn btn-default  btn-sm" style="margin-right:15px;">维修状态</button>',
						'<button type="button" class="RoleOfB btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',
						'<button type="button" class="RoleOfC btn btn-default  btn-sm" style="margin-right:15px;">详情</button>',

	        	        ].join('');
	            }
        }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
	
	//修改维修状态
	$('#updateFamilyTypeForm').bootstrapValidator({
        fields: {
        	updatefaultState: {validators: {notEmpty: {message: '维修状态不能为空'}}},
        },
        submitHandler: function (validator, form, submitButton) {
        	var serviceMan = $("#updateName").val();
//        	var a = serviceMan.indexOf("-");
//    		var serviceName = serviceMan.substr(a+1);//维修人员的名称
//        	var repairId = parseInt(serviceMan.substr(0,a));//维修人员的ID
        	var	params = {
					rowId:rowIdRet,
					faultState:$("#updatefaultState").val(),
					repairNumber :repairNumber,
        			repairTitle : repairTitle,
        			repairDate : repairDate,
		   		 	repairDateDoor : repairDateDoor,
		   		 	openId : openId,
		   		 	familyName : familyName,
		   		 	hInfo : hInfo,
		   		 	repairId : $("#updateName").val()
		   		 	
    			};
    		AjaxPostUtil.request({url:path+"95ba2c0a229947cbb21331f864062732",params:params,type:'json',callback:function(json){
    			if(json.returnCode==0){
    				qiao.bs.msg({msg:"保存成功",type:'success'});
    				$("#updateFamilyTypeMention").modal("hide");
    				location.reload();
//    				$('#messageTable').bootstrapTable('refresh',{url:path+'e4e7f020a08e4973b9d8870ee57e7f46'});
    			}else{
    				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
    			}
    		}});
    		return false;
        }
    });
	
	
	
	//删除
	function deleteFault(rowId){
		AjaxPostUtil.request({url:path+"5b0c2f1da23547c383ea15ce0fe506cc",params:{rowId:rowId},type:'json',callback:function(json){
			if(json.returnCode==0){
				qiao.bs.msg({msg:"删除成功",type:'success'});
				$('#messageTable').bootstrapTable('refresh',{url:path+'e4e7f020a08e4973b9d8870ee57e7f46'});
			}else{
				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			}
			
			
			
		}});
	}
}

function editRepair(rowId){
	AjaxPostUtil.request({url : path+"29272a18223c4a198a8cd453eadbe31b", params : {}, type : 'json', callback : function(json){
		if(json.returnCode == 0){
			console.log(json);
			var source = $("#selectServiceMan").html();
			var template = Handlebars.compile(source);
			$("#updateName").html(template(json));
			AjaxPostUtil.request({url:path+"5194249c16a649e596c2d92e13ad8a40",params:{rowId:rowId},type:'json',callback:function(jsonMation){
				console.log(jsonMation)
				if(jsonMation.returnCode==0){
					console.log(jsonMation);
					rowIdRet = jsonMation.bean.id;
					$("#updatefaultState").val(jsonMation.bean.faultState);
					if(jsonMation.bean.faultState == 2){
						$("#serviceManDiv").removeClass("hide");
					}else{
						$("#serviceManDiv").addClass("hide");
					}
					$("#updateName").val(jsonMation.bean.sId);
					$("#updateFamilyTypeMention").modal("show");
				}else{
					qiao.bs.msg({msg:jsonMation.returnMessage,type:'danger'});
				}
			}});
		}else{
			qiao.bs.msg({msg : json.returnMessage, type : 'danger'});
		}
	}});
	
	
		
}

function findRepairInfo(rowId){
	AjaxPostUtil.request({url:path+"5194249c16a649e596c2d92e13ad8a40",params:{rowId:rowId},type:'json',callback:function(json){
		console.log(json)
		if(json.returnCode==0){
			console.log(json);
			rowIdRet = json.bean.id;
			$("#findrepairNumber").val(json.bean.repairNumber);
			$("#findrepairTitle").val(json.bean.repairTitle);
			$("#findrepairText").val(json.bean.repairText);
			$("#findfamilyName").val(json.bean.familyName);
			$("#findhInfo").val(json.bean.hInfo);
			$("#findcreateTime").val(json.bean.createTime);
			var noticeTime = json.bean.noticeTime;
			if(isNull(noticeTime)){
				noticeTime='--';
			}
			$("#findnoticeTime").val(noticeTime);
			
			var serviceName = json.bean.name;
			if(isNull(serviceName)){
				serviceName='--';
			}
			$("#findserviceName").val(serviceName);
			
			var repairTime = json.bean.repairTime;
			if(isNull(repairTime)){
				repairTime='--';
			}
			$("#findrepairTime").val(repairTime);
			
			var repairDate = json.bean.rTime;
			if(repairDate=="0 0"){
				repairDate = '--'
			}
			$("#findrepairDate").val(repairDate);
			
			var type = json.bean.type;
			if(type==1){
				$("#findrepairType").val("个人报修");
			}else if(type == 2){
				$("#findrepairType").val("公共报修");
    		}

			$("#findRepairInfo").modal("show");
		}else{
			qiao.bs.msg({msg:json.returnMessage,type:'danger'});
		}
	}});
	
}


function eventInt(){
	
	//新增按钮时打开模态框,并获取楼号
	$('body').on('click', '#addFault', function(e){
		//$("#addFaultMation").modal("show");
		location.href = "../../html/wechat_repair/wechat_repair.html";
		
	});	
	
	$('body').on('change', '#updatefaultState', function(e){
		var FaultState = $("#updatefaultState").val();
		
		if(FaultState == 2){
			$("#serviceManDiv").removeClass("hide");
			
			AjaxPostUtil.request({url : path+"29272a18223c4a198a8cd453eadbe31b", params : {}, type : 'json', callback : function(json){
				if(json.returnCode == 0){
					console.log(json);
					var source = $("#selectServiceMan").html();
					var template = Handlebars.compile(source);
					$("#updateName").html(template(json));
				}else{
					$("#serviceManDiv").addClass("hide");
					qiao.bs.msg({msg : json.returnMessage, type : 'danger'});
				}
			}});
		}
		if(FaultState==1){
			$("#serviceManDiv").addClass("hide");
		}
		if(FaultState==3){
			$("#serviceManDiv").addClass("hide");
		}
		
	});
	
	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"e4e7f020a08e4973b9d8870ee57e7f46", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'e4e7f020a08e4973b9d8870ee57e7f46'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
	
	
	//刷新表单
	$('body').on('click', '#refreshDiv', function(e){
		$('#messageTable').bootstrapTable('refresh',{url : path+'e4e7f020a08e4973b9d8870ee57e7f46'});
		qiao.bs.msg({msg : "刷新成功.", type : 'success'});
	});
}

